import { groups, createGroup, follow, cancelFollow } from '../../api/user'
import Notify from '../../miniprogram_npm/@vant/weapp/notify/notify'

Component({
  properties: {
    detail: {
      type: Object,
      value: {}
    },
    isConsult: {
      type: Boolean,
      value: false
    },
    isAuth: {
      type: Boolean,
      value: true
    },
    needUserStatistics: {
      type: Boolean,
      value: true
    }
  },

  lifetimes: {
    attached() {
      this.getGroupList()
    }
  },

  attached() {
    this.getGroupList()
  },

  data: {
    groups: [],
    showGroups: false,
    showDialog: false,
    groupName: '',
    info: {}
  },

  observers: {
    'detail': function(detail) {
      if (typeof detail.owner === 'undefined') {
        if (typeof detail.isCurrent === 'undefined') {
          detail.isCurrent = true
        }
      } else {
        detail.isCurrent = detail.owner
      }
      if (detail.goodAtLabels && detail.goodAtLabels.length > 2) {
        detail.goodAtLabels.splice(2, detail.goodAtLabels.length - 2)
      }
      if (detail.trainSpecialityLabels && detail.trainSpecialityLabels.length > 2) {
        detail.trainSpecialityLabels.splice(2, detail.trainSpecialityLabels.length - 2)
      }
      this.setData({ info: detail })
    }
  },

  methods: {
    getGroupList() {
      groups().then(res => {
        this.setData({
          groups: res
        })
      })
    },
    followTeacher() {
      this.setData({
        showGroups: true
      })
      this.triggerEvent('stopScroll')
    },
    unFollowTeacher() {
      cancelFollow({
        followId: this.data.info.followId
      }).then(res => {
        this.setData({
          'info.isFollowed': false
        })
      })
    },
    unSelectGroup() {
      this.setData({
        showGroups: false
      })
      this.triggerEvent('startScroll')
    },
    selectGroup(e) {
      follow({
        followUserId: this.data.info.userId,
        groupId: e.currentTarget.dataset.id
      }).then(res => {
        this.setData({
          showGroups: false,
          'info.isFollowed': true,
          'info.followId': res
        })
      })
    },
    addGroup(){
      this.setData({
        showDialog: true
      })
    },
    confirmCreate() {
      if (!this.data.groupName) {
        Notify({ type: 'danger', message: '请输入分组名' })
      } else {
        createGroup({ groupName: this.data.groupName }).then(res => {
          let groups = this.data.groups
          groups.push({
            id: res,
            name: this.data.groupName,
            special: false
          })
          this.setData({
            groups: groups,
            groupName: '',
            showDialog: false
          })
        })
      }
    },
    closeDialog() {
      this.setData({ showDialog: false });
    },
    setGroupName(e) {
      this.setData({
        groupName: e.detail.value
      })
    },
    consult() {
      wx.navigateTo({
        url: '/packageA/pages/msgRecords/msgRecords?userId=' + this.data.detail.userId
      })
    }
  }
})
